$(function () {

    var edit_dialog = $('#edit_dialog');
    var editp_dialog = $('#editp_dialog');
    var edit_form = $('#edit_form');
    var edit_tabs = $('#edit_tabs');
    var staff_datagrid = $('#staff_datagrid');
    //数据表格
    staff_datagrid.datagrid({
        url: '/staff/list.do',
        striped: true,
        singleSelect: true,
        pagination: true,
        rownumbers: true,
        toolbar: "#tb",
        columns: [[
            {field: 'cb', checkbox: true},
            {field: 'id', hidden: true, width: 100},
            {field: 'username', title: '职工名称', width: 100},
            {field: 'dept', title: '所属部门', width: 100, align: 'right', formatter: deptFormatter},
            {field: 'gender', title: '性别', width: 100, align: 'right', formatter: genderFormatter},
            {field: 'age', title: '年龄', width: 100, align: 'right'},
            {field: 'studyeffort', title: '学历', width: 100, align: 'right', formatter: studyFormatter},
            {field: 'family', title: '祖籍', width: 100, align: 'right'},
            {field: 'borndate', title: '出生年月', width: 100, align: 'right'},
            {field: 'political', title: '政治面貌', width: 100, align: 'right', formatter: politicalFormatter},
            {field: 'tel', title: '联系电话', width: 100, align: 'right'},
            {field: 'hiredate', title: '入职日期', width: 100, align: 'right'},
            {field: 'seniroity', title: '工龄', width: 100, align: 'right'},
            {field: 'marriage', title: '婚姻状况', width: 100, align: 'right', formatter: marriageFormatter},
            {field: 'state', title: '状态', width: 100, align: 'right', formatter: stateFormatter},
            {field: 'admin', title: '超级管理员', width: 100, align: 'right', formatter: adminFormatter},
            {field: 'duty', title: '职务', width: 100, align: 'right', formatter: dutyFormatter},
            {field: 'hiretype', title: '聘用类型', width: 100, align: 'right', formatter: hiretypeFoamatter},
            {field: 'timecard', title: '考勤卡号', width: 100, align: 'right'},
            {field: 'idcard', title: '身份证号', width: 100, align: 'right'},
            {field: 'jobnumber', title: '工号', width: 100, align: 'right'},
            {field: 'password', title: '密码', width: 100, align: 'right'},
            {field:'portrait',title:'照片',width:100,align:'right',formatter:imgformatter}
        ]]

    });
    //end
    var cmdObj = {
        //添加按钮添加部门
        add: function () {

            edit_dialog.dialog("setTitle", "添加职工");

            edit_form.form("clear");

            edit_dialog.dialog("open");
        },
        //编辑按钮编辑职工
        edit: function () {

            var row = staff_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要编辑的职工", "info");
                return;
            }

            edit_dialog.dialog("setTitle", "编辑职工");
            //在读取数据之前,清除缓存
            edit_form.form("clear");

            //加工回显数据
            if (row.dept) {
                row["dept.id"] = row.dept.id;
            }
            if (row.studyeffort) {
                row["studyeffort.id"] = row.studyeffort.id;
            }
            if (row.political) {
                row["political.id"] = row.political.id;
            }
            if (row.marriage) {
                row["marriage.id"] = row.marriage.id;
            }
            if (row.duty) {
                row["duty.id"] = row.duty.id;
            }
            if (row.hiretype) {
                row["hiretype.id"] = row.hiretype.id;
            }
            if (row.gender) {
                row["gender.id"] = row.gender.id;
            }
            edit_form.form("load", row);

            edit_dialog.dialog("open");
            //图片回显
            $("#imgId").prop("src", getCellValue());
        },
        //save按钮提交表单
        save: function () {

            var url = "";

            var id = $("#staffId").val();
            if (id) {
                url = "/staff/update.do";
            } else {
                url = "/staff/save.do";
            }

            edit_form.form('submit', {
                url: url,
                success: function (data) {
                    var data = eval('(' + data + ')');
                    if (data.success) {
                        $.messager.confirm('温馨提示', '操作成功', function (r) {
                            edit_dialog.dialog("close");
                            staff_datagrid.datagrid("load");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg);
                    }
                }
            });
        },
        addp: function () {
            editp_dialog.dialog("open")
        },
        //编辑的关闭按钮功能
        cancel: function () {
            edit_dialog.dialog("close");
        },
        //刷新
        reload: function () {
            staff_datagrid.datagrid("reload");
        },
        //高级查询
        //加载职工列表时,将多余查询条件一起发起请求
        searchT: function () {
            //加载列表数据时,携带额外参数
            staff_datagrid.datagrid("load", {
                keyword: $("#keyword").val(),
                hiretypeId: $("#hiretype").val(),
                dutyId: $("#duty").val(),
                studyeffortId: $("#studyeffort").val()
            })
        }

    };

    //抽取优化
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");

        cmdObj[cmd]();
    });
    //end


    //dialog弹窗
    edit_dialog.dialog({
        title: 'My Dialog',
        buttons: "#bb",
        closed: true,
        width: 800,
        height: 450
    });
    //end
});//end

//部门格式化
function deptFormatter(value, row, index) {
    return value ? value.name : "";
}
//学历
function studyFormatter(value, row, index) {
    return value ? value.name : "";
}
//政治面貌
function politicalFormatter(value, row, index) {
    return value ? value.name : "";
}
//婚姻状况
function marriageFormatter(value, row, index) {
    return value ? value.name : "";
}
//职务状况
function dutyFormatter(value, row, index) {
    return value ? value.name : "";
}
//聘用类型状况
function hiretypeFoamatter(value, row, index) {
    return value ? value.name : "";
}
//性别类型状况
function genderFormatter(value, row, index) {
    return value ? value.name : "";
}

//格式化员工状态
function stateFormatter(value, row, index) {
    if (value == 0) {
        return "<span style='color: green'>" + "在职" + "</span>"
    } else if (value = 1) {
        return "<span style='color: red'>" + "离职" + "</span>"
    } else {
        return "待定";
    }
}

//格式化超级管理员
function adminFormatter(value, row, index) {
    return value ? "是" : "否";
}

function getCellValue() {
    var rows = $("#staff_datagrid").datagrid("getRows");
    var imgPath = null;
    for (var i = 0; i < rows.length; i++) {
        imgPath = rows[i]['imgPath'];
    }
    return imgPath;
}

function imgformatter(value, row, index) {
    if (value) {
        return "<img src='" + value + "' style='width: 120px; height: 100px'>";
    }

}